package com.niodata.dp.collection;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class WrappedList<T> extends JsonSerializable.Base implements Serializable {

  private List<T> list;

  public WrappedList() {
  }

  public WrappedList(List<T> list) {
    this.list = list;
  }

  public static WrappedList emptyList() {
    return new WrappedList(Collections.emptyList());
  }

  public static <C extends WrappedList> C ofList(Class<C> clazz, Object... values) {
    WrappedList list = null;
    try {
      list = clazz.newInstance();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    list.setList(Arrays.asList(values));
    return (C) list;
  }

  public List<T> getList() {
    return list;
  }

  public void setList(List<T> list) {
    this.list = list;
  }

  @Override
  public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeObject(list);
  }

  @Override
  public void serializeWithType(
        JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer)
        throws IOException {
    gen.writeObject(list);
  }
}
